from collections import OrderedDict

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


# 路由访问方式，page第一页，page_size每页显示数量
class TagListPager(PageNumberPagination):
    # page_size = 10  # 每页几条数据
    page_size_query_param = 'page_size'  # 前端请求的第几页参数字段

    # 自定义分页形式
    def get_paginated_response(self, data):
        # OrderedDict字典的子类，生成有序的字典
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            ('page_range', list(self.page.paginator.page_range)),  # 页码范围
            ('has_next', self.page.has_next()),
            ('has_last', self.page.has_previous()),
            ('results', data)
        ]))
